package com.intretech.umsin.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intretech.umsin.demo.entity.UserEntity;
import com.intretech.umsin.demo.mapper.UserMapper;
import com.intretech.umsin.demo.param.UserParam;
import com.intretech.umsin.demo.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
* @version V1.0
* 用户:user 对应商业服务实现.
* @ClassName: UserServiceImpl
* @Description: 用户:user 对应商业服务实现.
* @author: 李启联
* @date:   2022-4-22 16:33:59
* @Copyright: Copyright (c) 2022
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {


    /**
    * 分页查询 用户 信息
    * @param page 分页对象
    * @param param 查询参数
    * @return IPage<UserEntity> 分页对象
    */
    @Override
    public IPage<UserEntity> queryForPage(IPage page, UserParam param){
        IPage<UserEntity> pageResult = this.page(page,createQueryWrapper(param));
        return pageResult;
    }

    /**
    * 根据条件查询返回 用户 对象列表
    * @param  param 查询参数
    * @return 用户 对象列表
    */
    @Override
    public List<UserEntity> queryForList(UserParam param) {
        List<UserEntity> list = this.list(createQueryWrapper(param));
        return list;
    }

    /**
    * 构建查询条件
    * @param param 查询参数实体对象
    * @return QueryWrapper
    */
    private QueryWrapper<UserEntity> createQueryWrapper(UserParam param){
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(param)) { return queryWrapper; }
        Optional<UserParam> op = Optional.of(param);
        queryWrapper.lambda()
                .eq(op.map(UserParam::getId).filter(StringUtils::isNotBlank).isPresent(),UserEntity::getId,param.getId())
                .eq(op.map(UserParam::getName).filter(StringUtils::isNotBlank).isPresent(),UserEntity::getName,param.getName())
                .eq(op.map(UserParam::getAge).isPresent(),UserEntity::getAge,param.getAge())
                .eq(op.map(UserParam::getSex).isPresent(),UserEntity::getSex,param.getSex())
                .eq(op.map(UserParam::getBirthday).isPresent(),UserEntity::getBirthday,param.getBirthday())
        ;
        return queryWrapper;
    }
	
    /**
     * 添加用户到数据库
     *
     * @param entity 实体对象
     * @return 添加后的实体对象
     */
    @Override
    public UserEntity add(UserEntity entity) {
        if (ObjectUtils.isEmpty(entity)) {return null;}
        if(this.save(entity)){
            return this.getById(entity.getId()) ;
        }else {
            return null;
        }
    }

    /**
     * 添加用户集合列表到数据库
     *
     * @param entities 实体对象列表
     * @return 添加后的实体对象集合列表
     */
    @Override
    public List<UserEntity> adds(List<UserEntity> entities) {
        if (CollectionUtils.isEmpty(entities)) { return null; }
        if(this.saveBatch(entities)){
            List<Long> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return this.listByIds(ids);
        }else {
           return null;
        }
    }

    /**
     * 更新用户到数据库
     *
     * @param entity 要更新实体对象
     * @return 更新后的实体对象
     */
    @Override
    public UserEntity modify(UserEntity entity) {
        if (ObjectUtils.isEmpty(entity)) {return null;}
        if(this.updateById(entity)){
            return this.getById(entity.getId()) ;
        }else {
            return null;
        }
    }

    /**
     * 更新用户集合列表到数据库
     *
     * @param entities 要更新实体对象集合
     * @return 更新后的实体对象集合
     */
    @Override
    public List<UserEntity> modifys(List<UserEntity> entities) {
        if (CollectionUtils.isEmpty(entities)) { return null; }
        if(this.updateBatchById(entities)){
            List<Long> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return this.listByIds(ids);
        }else {
            return null;
        }
    }	

}

